/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MonthEndDialog.java
 *
 * Created on 09-Mar-2011, 22:05:05
 */

package TimeRecording.forms;

import TimeRecording.TimeRecordingView;
import TimeRecording.business.DateRange;
import TimeRecording.exceptions.NoMonthEndException;
import TimeRecording.reporting.CollationReport;
import TimeRecording.reporting.MonthEndSummaryReport;
import TimeRecording.tables.AllocationOfHours;
import TimeRecording.tables.Employee;
import TimeRecording.tables.MonthlyControl;
import TimeRecording.tables.MonthlyTime;
import TimeRecording.widgets.EmployeeComboBox;
import java.awt.Cursor;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.persistence.EntityManager;
import javax.swing.JOptionPane;
import org.jdesktop.application.Action;

/**
 *
 * @author david
 */
public class MonthEndDialog extends javax.swing.JDialog {

    private TimeRecordingView myParent;
    private EntityManager entityManager;

    private Employee selectedEmployee;
    private MonthlyControl monthlyControl;
    private MonthlyTime monthlyTime;

    private AllocationOfHours liveHours;
    private AllocationOfHours snappedHours;
    private AllocationOfHours deltaHours;
    private AllocationOfHours emptyHours = new AllocationOfHours();

    private final static MonthlyTime nullMonthlyTime = new MonthlyTime();

    public Employee getSelectedEmployee() {
        return selectedEmployee;
    }

    public void setSelectedEmployee(Employee selectedEmployee) {
        Employee oldSelectedEmployee = this.selectedEmployee;
        this.selectedEmployee = selectedEmployee;
        firePropertyChange("selectedEmployee", oldSelectedEmployee, selectedEmployee);
    }

    public AllocationOfHours getDeltaHours() {
        return deltaHours;
    }

    public void setDeltaHours(AllocationOfHours deltaHours) {
        AllocationOfHours oldHours = this.deltaHours;
        this.deltaHours = deltaHours;
        firePropertyChange("deltaHours", oldHours, deltaHours);
    }

    public AllocationOfHours getLiveHours() {
        return liveHours;
    }

    public void setLiveHours(AllocationOfHours liveHours) {
        AllocationOfHours oldHours = this.liveHours;
        this.liveHours = liveHours;
        firePropertyChange("liveHours", oldHours, liveHours);
    }

    public AllocationOfHours getSnappedHours() {
        return snappedHours;
    }

    public void setSnappedHours(AllocationOfHours snappedHours) {
        AllocationOfHours oldHours = this.snappedHours;
        this.snappedHours = snappedHours;
        firePropertyChange("snappedHours", oldHours, snappedHours);
    }

    public AllocationOfHours getEmptyHours() {
        return emptyHours;
    }

    public void setEmptyHours(AllocationOfHours emptyHours) {
        AllocationOfHours oldHours = this.emptyHours;
        this.emptyHours = emptyHours;
        firePropertyChange("emptyHours", oldHours, emptyHours);
    }

    public MonthlyControl getMonthlyControl() {
        return monthlyControl;
    }

    public void setMonthlyControl(MonthlyControl monthlyControl) {

        MonthlyControl oldMonthlyControl = this.monthlyControl;
        this.monthlyControl = monthlyControl;
        firePropertyChange("monthlyControl", oldMonthlyControl, monthlyControl);
    }

    public MonthlyTime getMonthlyTime() {
        return monthlyTime;
    }

    public void setMonthlyTime(MonthlyTime monthlyTime) {

        MonthlyTime oldMonthlyTime = this.monthlyTime;
        this.monthlyTime = monthlyTime;
        firePropertyChange("monthlyTime", oldMonthlyTime, monthlyTime);

        if (monthlyTime == null) {
            setLiveHours(null);
            setSnappedHours(null);
            setDeltaHours(null);
            
        } else {
            setLiveHours(monthlyTime.getAllocation());
            setSnappedHours(monthlyTime.getRevisedAllocation());

            AllocationOfHours delta =
                    (AllocationOfHours) getLiveHours().clone();
            delta.subtract(getSnappedHours());
            setDeltaHours(delta);
        }
    }

    /** Creates new form MonthEndDialog */
    public MonthEndDialog(TimeRecordingView parent, EntityManager entityManager)
            throws NoMonthEndException {

        super(parent.getFrame(), false);

        myParent = parent;
        this.entityManager = entityManager;

        initComponents();

        monthlyControl = MonthlyControl.getLatest(entityManager);
        if (monthlyControl != null) {
            startDate.setValue(monthlyControl.getStartDate());
            endDate.setValue(monthlyControl.getEndDate());
            sessionalStartDate.setValue(monthlyControl.getSessionalStartDate());
            sessionalEndDate.setValue(monthlyControl.getSessionalEndDate());
        } else {
            JOptionPane.showMessageDialog(this,
                    "There is no month end data",
                    "Month End Figures", JOptionPane.INFORMATION_MESSAGE);
            throw new NoMonthEndException();
        }
    }

    @Action
    public void prevButtonClicked() {

        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime((Date)startDate.getValue());
        cal.add(GregorianCalendar.DATE, -1);
        
        MonthlyControl newMonthlyControl = MonthlyControl.get(
                entityManager, cal.getTime(), false);

        if (newMonthlyControl != null) {
            setMonthlyControl(newMonthlyControl);
            startDate.setValue(monthlyControl.getStartDate());
            endDate.setValue(monthlyControl.getEndDate());
            sessionalStartDate.setValue(monthlyControl.getSessionalStartDate());
            sessionalEndDate.setValue(monthlyControl.getSessionalEndDate());
        }

        populateTable();
    }

    @Action
    public void nextButtonClicked() {

        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime((Date)endDate.getValue());
        cal.add(GregorianCalendar.DATE, 1);

        MonthlyControl newMonthlyControl = MonthlyControl
                .get(entityManager, cal.getTime(), true);

        if (newMonthlyControl != null) {
            setMonthlyControl(newMonthlyControl);
            startDate.setValue(monthlyControl.getStartDate());
            endDate.setValue(monthlyControl.getEndDate());
            sessionalStartDate.setValue(monthlyControl.getSessionalStartDate());
            sessionalEndDate.setValue(monthlyControl.getSessionalEndDate());
        }
        
        populateTable();
    }

    @Action
    public void prevEmployeeClicked() {
        int index = employeeComboBox.getSelectedIndex() - 1;
        if (index >= 0) {
            employeeComboBox.setSelectedIndex(index);
            populateTable();
        }
    }

    @Action
    public void nextEmployeeClicked() {
        int index = employeeComboBox.getSelectedIndex() + 1;
        if (index < employeeComboBox.getItemCount()) {
            employeeComboBox.setSelectedIndex(index);
            populateTable();
        }
    }

    @Action
    public void displayAsHoursAndMinutes() {
        snapshotAllocation.showHoursAndMinutes(true);
        liveAllocation.showHoursAndMinutes(true);
        deltaAllocation.showHoursAndMinutes(true);
    }

    @Action
    public void displayAsHours() {
        snapshotAllocation.showHoursAndMinutes(false);
        liveAllocation.showHoursAndMinutes(false);
        deltaAllocation.showHoursAndMinutes(false);
    }

    private void populateTable() {

        if (selectedEmployee != null && monthlyControl != null) {
            setMonthlyTime(MonthlyTime.get(entityManager,
                    monthlyControl.getStartDate(), selectedEmployee));
        } else {
            setMonthlyTime(null);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        timeDisplayGroup = new javax.swing.ButtonGroup();
        navigatorPanel = new javax.swing.JPanel();
        timeFramePanel = new javax.swing.JPanel();
        startDate = new javax.swing.JFormattedTextField();
        endDate = new javax.swing.JFormattedTextField();
        sessionalStartDate = new javax.swing.JFormattedTextField();
        sessionalEndDate = new javax.swing.JFormattedTextField();
        nonSessionalLabel = new javax.swing.JLabel();
        sessionalLabel = new javax.swing.JLabel();
        prevMonthButton = new javax.swing.JButton();
        nextMonthButton = new javax.swing.JButton();
        salariedReportButton = new javax.swing.JButton();
        sessionalReportButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        employeePanel = new javax.swing.JPanel();
        prevEmployeeButton = new javax.swing.JButton();
        employeeComboBox = new EmployeeComboBox(entityManager);
        nextEmployeeButton = new javax.swing.JButton();
        timeUnitsPanel = new javax.swing.JPanel();
        hoursAndMinutesToggle = new javax.swing.JToggleButton();
        hoursToggle = new javax.swing.JToggleButton();
        detailsPanel = new javax.swing.JTabbedPane();
        liveAllocation = new TimeRecording.forms.AllocationOfHoursPanel();
        snapshotAllocation = new TimeRecording.forms.AllocationOfHoursPanel();
        deltaAllocation = new TimeRecording.forms.AllocationOfHoursPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(TimeRecording.TimeRecordingApp.class).getContext().getResourceMap(MonthEndDialog.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });
        getContentPane().setLayout(new java.awt.GridBagLayout());

        navigatorPanel.setName("navigatorPanel"); // NOI18N
        navigatorPanel.setLayout(new java.awt.GridBagLayout());

        timeFramePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("timeFramePanel.border.title"))); // NOI18N
        timeFramePanel.setName("timeFramePanel"); // NOI18N
        timeFramePanel.setLayout(new java.awt.GridBagLayout());

        startDate.setEditable(false);
        startDate.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/MM/yyyy"))));
        startDate.setMinimumSize(new java.awt.Dimension(40, 20));
        startDate.setName("startDate"); // NOI18N
        startDate.setPreferredSize(new java.awt.Dimension(70, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        timeFramePanel.add(startDate, gridBagConstraints);

        endDate.setEditable(false);
        endDate.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/MM/yyyy"))));
        endDate.setMinimumSize(new java.awt.Dimension(40, 20));
        endDate.setName("endDate"); // NOI18N
        endDate.setPreferredSize(new java.awt.Dimension(70, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        timeFramePanel.add(endDate, gridBagConstraints);

        sessionalStartDate.setEditable(false);
        sessionalStartDate.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/MM/yyyy"))));
        sessionalStartDate.setMinimumSize(new java.awt.Dimension(40, 20));
        sessionalStartDate.setName("sessionalStartDate"); // NOI18N
        sessionalStartDate.setPreferredSize(new java.awt.Dimension(70, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        timeFramePanel.add(sessionalStartDate, gridBagConstraints);

        sessionalEndDate.setEditable(false);
        sessionalEndDate.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/MM/yyyy"))));
        sessionalEndDate.setMinimumSize(new java.awt.Dimension(40, 20));
        sessionalEndDate.setName("sessionalEndDate"); // NOI18N
        sessionalEndDate.setPreferredSize(new java.awt.Dimension(70, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        timeFramePanel.add(sessionalEndDate, gridBagConstraints);

        nonSessionalLabel.setText(resourceMap.getString("nonSessionalLabel.text")); // NOI18N
        nonSessionalLabel.setName("nonSessionalLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        timeFramePanel.add(nonSessionalLabel, gridBagConstraints);

        sessionalLabel.setText(resourceMap.getString("sessionalLabel.text")); // NOI18N
        sessionalLabel.setName("sessionalLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        timeFramePanel.add(sessionalLabel, gridBagConstraints);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(TimeRecording.TimeRecordingApp.class).getContext().getActionMap(MonthEndDialog.class, this);
        prevMonthButton.setAction(actionMap.get("prevButtonClicked")); // NOI18N
        prevMonthButton.setText(resourceMap.getString("prevMonthButton.text")); // NOI18N
        prevMonthButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
        prevMonthButton.setName("prevMonthButton"); // NOI18N
        prevMonthButton.setPreferredSize(new java.awt.Dimension(32, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        timeFramePanel.add(prevMonthButton, gridBagConstraints);

        nextMonthButton.setAction(actionMap.get("nextButtonClicked")); // NOI18N
        nextMonthButton.setText(resourceMap.getString("nextMonthButton.text")); // NOI18N
        nextMonthButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
        nextMonthButton.setName("nextMonthButton"); // NOI18N
        nextMonthButton.setPreferredSize(new java.awt.Dimension(32, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        timeFramePanel.add(nextMonthButton, gridBagConstraints);

        salariedReportButton.setAction(actionMap.get("reportSalaried")); // NOI18N
        salariedReportButton.setText(resourceMap.getString("salariedReportButton.text")); // NOI18N
        salariedReportButton.setName("salariedReportButton"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        timeFramePanel.add(salariedReportButton, gridBagConstraints);

        sessionalReportButton.setAction(actionMap.get("reportSessional")); // NOI18N
        sessionalReportButton.setText(resourceMap.getString("sessionalReportButton.text")); // NOI18N
        sessionalReportButton.setName("sessionalReportButton"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        timeFramePanel.add(sessionalReportButton, gridBagConstraints);

        jButton1.setAction(actionMap.get("monthlySummaryReport")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        timeFramePanel.add(jButton1, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        navigatorPanel.add(timeFramePanel, gridBagConstraints);

        employeePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("employeePanel.border.title"))); // NOI18N
        employeePanel.setName("employeePanel"); // NOI18N
        employeePanel.setLayout(new java.awt.GridBagLayout());

        prevEmployeeButton.setAction(actionMap.get("prevEmployeeClicked")); // NOI18N
        prevEmployeeButton.setText(resourceMap.getString("prevEmployeeButton.text")); // NOI18N
        prevEmployeeButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
        prevEmployeeButton.setName("prevEmployeeButton"); // NOI18N
        prevEmployeeButton.setPreferredSize(new java.awt.Dimension(32, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        employeePanel.add(prevEmployeeButton, gridBagConstraints);

        employeeComboBox.setMinimumSize(new java.awt.Dimension(100, 18));
        employeeComboBox.setName("employeeComboBox"); // NOI18N
        employeeComboBox.setPreferredSize(new java.awt.Dimension(200, 20));

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${selectedEmployee}"), employeeComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        employeeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                employeeSelected(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        employeePanel.add(employeeComboBox, gridBagConstraints);

        nextEmployeeButton.setAction(actionMap.get("nextEmployeeClicked")); // NOI18N
        nextEmployeeButton.setText(resourceMap.getString("nextEmployeeButton.text")); // NOI18N
        nextEmployeeButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
        nextEmployeeButton.setName("nextEmployeeButton"); // NOI18N
        nextEmployeeButton.setPreferredSize(new java.awt.Dimension(32, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        employeePanel.add(nextEmployeeButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        navigatorPanel.add(employeePanel, gridBagConstraints);

        timeUnitsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("timeUnitsPanel.border.title"))); // NOI18N
        timeUnitsPanel.setName("timeUnitsPanel"); // NOI18N
        timeUnitsPanel.setLayout(new java.awt.GridBagLayout());

        hoursAndMinutesToggle.setAction(actionMap.get("displayAsHoursAndMinutes")); // NOI18N
        timeDisplayGroup.add(hoursAndMinutesToggle);
        hoursAndMinutesToggle.setSelected(true);
        hoursAndMinutesToggle.setText(resourceMap.getString("hoursAndMinutesToggle.text")); // NOI18N
        hoursAndMinutesToggle.setMargin(new java.awt.Insets(2, 2, 2, 2));
        hoursAndMinutesToggle.setMaximumSize(new java.awt.Dimension(111, 23));
        hoursAndMinutesToggle.setMinimumSize(new java.awt.Dimension(70, 23));
        hoursAndMinutesToggle.setName("hoursAndMinutesToggle"); // NOI18N
        hoursAndMinutesToggle.setPreferredSize(new java.awt.Dimension(70, 23));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        timeUnitsPanel.add(hoursAndMinutesToggle, gridBagConstraints);

        hoursToggle.setAction(actionMap.get("displayAsHours")); // NOI18N
        timeDisplayGroup.add(hoursToggle);
        hoursToggle.setText(resourceMap.getString("hoursToggle.text")); // NOI18N
        hoursToggle.setMargin(new java.awt.Insets(2, 2, 2, 2));
        hoursToggle.setMaximumSize(new java.awt.Dimension(111, 23));
        hoursToggle.setMinimumSize(new java.awt.Dimension(70, 23));
        hoursToggle.setName("hoursToggle"); // NOI18N
        hoursToggle.setPreferredSize(new java.awt.Dimension(70, 23));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        timeUnitsPanel.add(hoursToggle, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        navigatorPanel.add(timeUnitsPanel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 4);
        getContentPane().add(navigatorPanel, gridBagConstraints);

        detailsPanel.setName("detailsPanel"); // NOI18N

        liveAllocation.setName("liveAllocation"); // NOI18N
        liveAllocation.setPreferredSize(new java.awt.Dimension(64, 202));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${liveHours}"), liveAllocation, org.jdesktop.beansbinding.BeanProperty.create("allocationOfHours"));
        binding.setSourceNullValue(nullMonthlyTime.getRevisedAllocation());
        binding.setSourceUnreadableValue(nullMonthlyTime.getRevisedAllocation());
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${snappedHours}"), liveAllocation, org.jdesktop.beansbinding.BeanProperty.create("comparisonHours"));
        bindingGroup.addBinding(binding);

        detailsPanel.addTab(resourceMap.getString("liveAllocation.TabConstraints.tabTitle"), liveAllocation); // NOI18N

        snapshotAllocation.setName("snapshotAllocation"); // NOI18N
        snapshotAllocation.setPreferredSize(new java.awt.Dimension(64, 202));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${snappedHours}"), snapshotAllocation, org.jdesktop.beansbinding.BeanProperty.create("allocationOfHours"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${liveHours}"), snapshotAllocation, org.jdesktop.beansbinding.BeanProperty.create("comparisonHours"));
        bindingGroup.addBinding(binding);

        detailsPanel.addTab(resourceMap.getString("snapshotAllocation.TabConstraints.tabTitle"), snapshotAllocation); // NOI18N

        deltaAllocation.setName("deltaAllocation"); // NOI18N
        deltaAllocation.setPreferredSize(new java.awt.Dimension(64, 202));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${deltaHours}"), deltaAllocation, org.jdesktop.beansbinding.BeanProperty.create("allocationOfHours"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${emptyHours}"), deltaAllocation, org.jdesktop.beansbinding.BeanProperty.create("comparisonHours"));
        bindingGroup.addBinding(binding);

        detailsPanel.addTab(resourceMap.getString("deltaAllocation.TabConstraints.tabTitle"), deltaAllocation); // NOI18N

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        getContentPane().add(detailsPanel, gridBagConstraints);

        bindingGroup.bind();

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-879)/2, (screenSize.height-428)/2, 879, 428);
    }// </editor-fold>//GEN-END:initComponents

    private void employeeSelected(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_employeeSelected
        populateTable();
    }//GEN-LAST:event_employeeSelected

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        myParent.hideMonthEndDialog();
    }//GEN-LAST:event_formWindowClosing

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowOpened

    private void collatedTimeReport(DateRange dateRange,
            boolean sessional, boolean salaried) {

        SelectEmployeesDialog selectEmployees =
                new SelectEmployeesDialog(this, entityManager, true);
        if (selectedEmployee != null) {
            selectEmployees.select(selectedEmployee);
        }
        selectEmployees.setVisible(true);

        if (selectEmployees.getSelectedEmployees().isEmpty()) {
            return;
        }

        Cursor savedCursor = getCursor();
        try {
            setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            new CollationReport().periodReport(dateRange,
                    selectEmployees.getSelectedEmployees(), entityManager,
                    sessional, salaried);
        } finally {
            setCursor(savedCursor);
        }
    }

    @Action
    public void reportSalaried() {
        
        collatedTimeReport(new DateRange(
                monthlyControl.getStartDate(),
                monthlyControl.getEndDate()),
                false, true);
    }
    
    @Action
    public void reportSessional() {

        collatedTimeReport(new DateRange(
                monthlyControl.getSessionalStartDate(),
                monthlyControl.getSessionalEndDate()),
                true, false);
    }

    @Action
    public void monthlySummaryReport() {

        SelectEmployeesDialog selectEmployees =
                new SelectEmployeesDialog(this, entityManager, true);
        if (selectedEmployee != null) {
            selectEmployees.select(selectedEmployee);
        }
        selectEmployees.setVisible(true);

        if (selectEmployees.getSelectedEmployees().isEmpty()) {
            return;
        }

        Cursor savedCursor = getCursor();
        try {
            setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            new MonthEndSummaryReport().viewReport(
                    selectEmployees.getSelectedEmployees(),
                    entityManager, monthlyControl.getSessionalStartDate());
        } finally {
            setCursor(savedCursor);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private TimeRecording.forms.AllocationOfHoursPanel deltaAllocation;
    private javax.swing.JTabbedPane detailsPanel;
    private javax.swing.JComboBox employeeComboBox;
    private javax.swing.JPanel employeePanel;
    private javax.swing.JFormattedTextField endDate;
    private javax.swing.JToggleButton hoursAndMinutesToggle;
    private javax.swing.JToggleButton hoursToggle;
    private javax.swing.JButton jButton1;
    private TimeRecording.forms.AllocationOfHoursPanel liveAllocation;
    private javax.swing.JPanel navigatorPanel;
    private javax.swing.JButton nextEmployeeButton;
    private javax.swing.JButton nextMonthButton;
    private javax.swing.JLabel nonSessionalLabel;
    private javax.swing.JButton prevEmployeeButton;
    private javax.swing.JButton prevMonthButton;
    private javax.swing.JButton salariedReportButton;
    private javax.swing.JFormattedTextField sessionalEndDate;
    private javax.swing.JLabel sessionalLabel;
    private javax.swing.JButton sessionalReportButton;
    private javax.swing.JFormattedTextField sessionalStartDate;
    private TimeRecording.forms.AllocationOfHoursPanel snapshotAllocation;
    private javax.swing.JFormattedTextField startDate;
    private javax.swing.ButtonGroup timeDisplayGroup;
    private javax.swing.JPanel timeFramePanel;
    private javax.swing.JPanel timeUnitsPanel;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}
